package com.yc.fav.servlet;

import com.yc.base.servlet.BaseServlet;
import com.yc.fav.entity.Tag;
import com.yc.fav.entity.User;
import com.yc.fav.mapper.TagMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.session.SqlSession;
import org.example.util.MyBatisUtils;

import java.io.IOException;
import java.util.List;

@WebServlet("/tag/*")
public class TagServlet extends BaseServlet {

    public Object myTags(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("loginedUser");
        if(user == null){
            return null;
        }
        try(SqlSession sqlSession = MyBatisUtils.getSession()){
            TagMapper tagMapper = sqlSession.getMapper(TagMapper.class);
            List<Tag> list = tagMapper.selectByUid(user.getId());
            return list;
        }
    }

    public Object queryById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int tid = Integer.parseInt(req.getParameter("tid"));
        User user = (User) req.getSession().getAttribute("loginedUser");
        if(user == null){
            return null;
        }
        try(SqlSession sqlSession = MyBatisUtils.getSession()){
            TagMapper tagMapper = sqlSession.getMapper(TagMapper.class);
            return tagMapper.queryById(tid, user.getId());
        }
    }
}
